route.ts 942 B

123456789101112131415161718192021222324252627
  1. import { NextRequest, NextResponse } from 'next/server';
  2. import { ResultDto } from '@/dtos/response/common';
  3. import { LoginResponse } from '@/dtos/response/auth';
  4. import { fetchJson } from '@/lib/utils/server';
  5. export async function POST(request: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
  6. const { path } = await params;
  7. const endpoint = `/api/auth/${path.join('/')}`;
  8. const body = await request.json();
  9. const res: ResultDto = await fetchJson(endpoint, {
  10. method: 'POST',
  11. body: JSON.stringify(body)
  12. });
  13. const response = NextResponse.json(res);
  14. // 로그인 성공 시 쿠키 설정
  15. if (path[0] === 'login' && res.success && res.data) {
  16. const data = res.data as LoginResponse;
  17. const cookieOptions = { httpOnly: true, path: '/' };
  18. response.cookies.set('accessToken', data.accessToken, cookieOptions);
  19. response.cookies.set('refreshToken', data.refreshToken, cookieOptions);
  20. }
  21. return response;
  22. }